home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Gfx / Edit / TSMorph / src / ag2txt / ag2txt.c
C/C++ Source or Header  |  1995-03-12  |  8KB  |  337 lines

  1. // This program is free software; you can redistribute it and/or modify
  2. // it under the terms of the GNU General Public License as published by
  3. // the Free Software Foundation; either version 2 of the License, or
  4. // any later version.
  5.  
  6. // This program is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. // GNU General Public License for more details.
  10.  
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software
  13. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14.  
  15. // mpaddock@cix.compulink.co.uk
  16.  
  17. // Convert .guide file (MakeGuide --amiga-39 ...) to .doc
  18.  
  19. // This program is a complete hack which will only work in specific cases!!!
  20. // It is based on AG2TXT from which the following is quoted:
  21.  
  22. // This program (even if it is small) is Copyright (c) 1993 Jason R. Hulance.
  23. // Apart from that the Gnu Public License holds.  You can contact me by E-mail
  24. // at "m88jrh@ecs.oxford.ac.uk".
  25.  
  26. #include <stdio.h>
  27. #include <proto/dos.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30.  
  31. enum {NONE, ATSIGN, TILLEND, BRAC, BRACSEND, TILLBRAC,
  32.      GOT_N, GOT_O, GOT_D, GOT_E, SKIPARG, NODE, NODESEND}
  33.     got;
  34.  
  35. enum {ERR_SHORT, ERR_MEM, ERR_IO, ERR_FILE, ERR_ARGS, ERR_NONE}
  36.     err;
  37.  
  38. enum {ARG_FROM, ARG_TO, ARG_NODEFONT, ARG_LINKFONT}
  39.     arg;
  40.  
  41. enum {NODEFONT, LINKFONT}
  42.     font;
  43.  
  44. #define QUOTE 34
  45. #define RET '\n'
  46. #define ESC 27
  47. #define FONT_BOLD '1'
  48. #define FONT_ITALIC '3'
  49. #define FONT_REVERSE '7'
  50. #define FONT_NORMAL '0'
  51. #define FONT_UNDERLINE '4'
  52.  
  53. void setfont(char * str,int ind);
  54.  
  55. char myfont[2] = {FONT_REVERSE,FONT_ITALIC};
  56.  
  57. UBYTE vers[]="$VER: ag2txt 3.2 (12.3.94)";
  58.  
  59. int main(void) {
  60.     int state=NONE;
  61.     int quote;
  62.     char *in;
  63.     char *out;
  64.     char *max;
  65.     LONG args[4] = {0,0,0,0};
  66.     BOOL ok = FALSE;
  67.     FILE *from,*to;
  68.     char *bin,*bout=NULL;
  69.     int len;
  70.     struct RDArgs *rdargs;
  71.     if (rdargs = ReadArgs("FROM/A,TO/A,NODEFONT=NODE/K,LINKFONT=LINK/K",
  72.                                     args,NULL)) {
  73.         if (from = fopen((char *)args[ARG_FROM],"r")) {
  74.             fseek(from,0,2);
  75.             if (len = ftell(from)) {
  76.                 rewind(from);
  77.                 if (to = fopen((char *)args[ARG_TO],"w")) {
  78.                     if (args[ARG_NODEFONT]) {
  79.                         setfont((char *)args[ARG_NODEFONT],NODEFONT);
  80.                     }
  81.                     if (args[ARG_LINKFONT]) {
  82.                         setfont((char *)args[ARG_LINKFONT],LINKFONT);
  83.                     }
  84.                     if ((bin = (char *)malloc(len)) && (bout = (char *)malloc(len))) {
  85.                         fread(bin,len,1,from);
  86.                         in = bin;
  87.                         out = bout;
  88.                         max = bin+len;
  89.                         // Skip till main Node
  90.                         while ((in<max) && (state == NONE)) {
  91.                             if ((*in == '@') && (in[1] == 'N')) {
  92.                                 state = ATSIGN;
  93.                             }
  94.                             in++;
  95.                         }
  96.                         if (in < max) {
  97.                             in--;
  98.                         }
  99.                         state = NONE;
  100.                         while (in<max) {
  101.                             switch (state) {
  102.                             case NONE:
  103.                                 if (*in == '@') {
  104.                                     if (in[1] == '{') {
  105.                                         state = BRAC;
  106.                                         in++;
  107.                                     }
  108.                                     else {
  109.                                         if ((in == bin) || (in[-1] == RET)) {
  110.                                             state = ATSIGN;
  111.                                         }
  112.                                         else {
  113.                                             *out++ = *in;
  114.                                         }
  115.                                     }
  116.                                 }
  117.                                 else {
  118.                                     *out++ = *in;
  119.                                 }
  120.                                 break;
  121.                             case ATSIGN:
  122.                                 if ((*in == 'N') || (*in == 'n')) {
  123.                                     state = GOT_N;
  124.                                 }
  125.                                 else {
  126.                                     state = TILLEND;
  127.                                 }
  128.                                 break;
  129.                             case TILLEND:
  130.                                 if (*in == RET) {
  131.                                     state = NONE;
  132.                                 }
  133.                                 break;
  134.                             case BRAC:
  135.                                 if (*in == 'b') {
  136.                                     *out++ = ESC;
  137.                                     *out++ = '[';
  138.                                     *out++ = FONT_BOLD;
  139.                                     *out++ = 'm';
  140.                                     in++;
  141.                                     state = NONE;
  142.                                 }
  143.                                 else {
  144.                                     if ((*in == 'u') && (in[1] != '}')) {
  145.                                         *out++ = ESC;
  146.                                         *out++ = '[';
  147.                                         *out++ = FONT_NORMAL;
  148.                                         *out++ = 'm';
  149.                                         in++;
  150.                                         in++;
  151.                                         state = NONE;
  152.                                     }
  153.                                     else {
  154.                                         if (*in == 'u') {
  155.                                             *out++ = ESC;
  156.                                             *out++ = '[';
  157.                                             *out++ = FONT_UNDERLINE;
  158.                                             *out++ = 'm';
  159.                                             in++;
  160.                                             state = NONE;
  161.                                         }
  162.                                         else {
  163.                                             if (*in == 'i') {
  164.                                                 *out++ = ESC;
  165.                                                 *out++ = '[';
  166.                                                 *out++ = FONT_ITALIC;
  167.                                                 *out++ = 'm';
  168.                                                 in++;
  169.                                                 state = NONE;
  170.                                             }
  171.                                             else {
  172.                                                 if (*in != ' ') {
  173.                                                     quote = (*in == QUOTE);
  174.                                                     *out++ = ESC;
  175.                                                     *out++ = '[';
  176.                                                     *out++ = myfont[LINKFONT];
  177.                                                     *out++ = 'm';
  178.                                                     state = BRACSEND;
  179.                                                 }
  180.                                             }
  181.                                         }
  182.                                     }
  183.                                 }
  184.                                 break;
  185.                             case BRACSEND:
  186.                                 if ((quote && (*in == QUOTE)) || (!quote && (*in == ' '))) {
  187.                                     *out++ = ESC;
  188.                                     *out++ = '[';
  189.                                     *out++ = FONT_NORMAL;
  190.                                     *out++ = 'm';
  191.                                     state = TILLBRAC;
  192.                                 }
  193.                                 else {
  194.                                     *out++ = *in;
  195.                                 }
  196.                                 break;
  197.                             case TILLBRAC:
  198.                                 if (*in == '}') {
  199.                                     // pad extra space (for V39)
  200.                                     if (in[1] != '.') {
  201.                                         *out++ = ' ';
  202.                                     }
  203.                                     state = NONE;
  204.                                 }
  205.                                 break;
  206.                             case GOT_N:
  207.                                 if ((*in == 'O') || (*in == 'o')) {
  208.                                     state = GOT_O;
  209.                                 }
  210.                                 else {
  211.                                     state = TILLEND;
  212.                                 }
  213.                                 break;
  214.                             case GOT_O:
  215.                                 if ((*in == 'D') || (*in == 'd')) {
  216.                                     state = GOT_D;
  217.                                 }
  218.                                 else {
  219.                                     state = TILLEND;
  220.                                 }
  221.                                 break;
  222.                             case GOT_D:
  223.                                 if ((*in == 'E') || (*in == 'e')) {
  224.                                     state = GOT_E;
  225.                                 }
  226.                                 else {
  227.                                     state = TILLEND;
  228.                                 }
  229.                                 break;
  230.                             case GOT_E:
  231.                                 if (*in != ' ') {
  232.                                     quote = (*in == QUOTE);
  233.                                     state = SKIPARG;
  234.                                 }
  235.                                 break;
  236.                             case SKIPARG:
  237.                                 if ((quote && (*in == QUOTE)) || (!quote && (*in == ' '))) {
  238.                                     state = NODE;
  239.                                 }
  240.                                 break;
  241.                             case NODE:
  242.                                 if (*in != ' ') {
  243.                                     quote = (*in == QUOTE);
  244.                                     *out++ = ESC;
  245.                                     *out++ = '[';
  246.                                     *out++ = myfont[NODEFONT];
  247.                                     *out++ = 'm';
  248.                                     state = NODESEND;
  249.                                 }
  250.                                 break;
  251.                             case NODESEND:
  252.                                 if ((quote && (*in == QUOTE)) || (!quote && (*in == ' ')) || (*in == RET)) {
  253.                                     *out++ = ESC;
  254.                                     *out++ = '[';
  255.                                     *out++ = FONT_NORMAL;
  256.                                     *out++ = 'm';
  257.                                     *out++ = RET;
  258.                                     *out++ = RET;
  259.                                     if (*in == RET) {
  260.                                         state = NONE;
  261.                                     }
  262.                                     else {
  263.                                         state = TILLEND;
  264.                                     }
  265.                                 }
  266.                                 else {
  267.                                     *out++ = *in;
  268.                                 }
  269.                                 break;
  270.                             default:
  271.                                 *out++ = *in;
  272.                                 break;
  273.                             }
  274.                             in++;
  275.                         }
  276.                         fwrite(bout,out - bout,1,to);
  277.                         ok = TRUE;
  278.                     }
  279.                     else {
  280.                         printf("Error allocating memory\n");
  281.                     }
  282.                     if (bin) {
  283.                         free(bin);
  284.                     }
  285.                     if (bout) {
  286.                         free(bout);
  287.                     }
  288.                     fclose(to);
  289.                 }
  290.                 else {
  291.                     printf("Error opening TO\n");
  292.                 }
  293.             }
  294.             else {
  295.                 printf("FROM empty\n");
  296.             }
  297.             fclose(from);
  298.         }
  299.         else {
  300.             printf("Error opening FROM\n");
  301.         }
  302.         FreeArgs(rdargs);
  303.     }
  304.     else {
  305.         printf("Error in ReadArgs\n");
  306.     }
  307.     if (ok) {
  308.         return 0;
  309.     }
  310.     else {
  311.         return 10;
  312.     }
  313. }
  314.  
  315. void setfont(char * str,int ind) {
  316.   if (!(stricmp(str, "PLAIN"))) {
  317.         myfont[ind]=FONT_NORMAL;
  318.         return;
  319.     }
  320.   if (!(stricmp(str, "ITALIC"))) {
  321.         myfont[ind]=FONT_ITALIC;
  322.         return;
  323.     }
  324.   if (!(stricmp(str, "REVERSE"))) {
  325.         myfont[ind]=FONT_REVERSE;
  326.         return;
  327.     }
  328.   if (!(stricmp(str, "BOLD"))) {
  329.         myfont[ind]=FONT_BOLD;
  330.         return;
  331.     }
  332.   if (!(stricmp(str, "UNDERLINE"))) {
  333.         myfont[ind]=FONT_UNDERLINE;
  334.         return;
  335.     }
  336. }
  337.